package com.hsnn.medstgmini.trading.pur.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.model.IsLongtimePlan;
import com.hsnn.medstgmini.trading.catalog.model.PurHospcatalog;
import com.hsnn.medstgmini.trading.pur.dao.PurPurPlanDao;
import com.hsnn.medstgmini.trading.pur.model.form.PurPlanForm;
import com.hsnn.medstgmini.trading.pur.service.PurPurPlanManager;
import com.hsnn.medstgmini.util.DicSysUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;
import com.hsnn.medstgmini.util.ParseNumber;
@Service("purPurPlanManager")
public class PurPurPlanManagerImpl implements PurPurPlanManager {
	@Autowired
	private PurPurPlanDao dao;
	
	@Resource(name = "sessionFactory")
	private SessionFactory factory;
	
	@SuppressWarnings("unchecked")
	public Pagination getPurHospCatalogList(Pagination page){
		page = dao.getPurHospCatalogList(page);
		List<Map<String,Object>> list = (List<Map<String,Object>>) page.getRows();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				Map<String,Object> map =list.get(i);
				map.put("proPriceLimit",map.get("proPriceLimit")==null?"":StringUtils.isNotBlank(map.get("proPriceLimit").toString())?ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("proPriceLimit").toString())):"");
				map.put("proPrice",map.get("proPrice")==null?"":StringUtils.isNotBlank(map.get("proPrice").toString())?ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("proPrice").toString())):"");
				if (null != map.get("goodsSource")){
					map.put("goodsSource",DicSysUtil.getValueByClassCodeAndDicCode(Constants.PRO_METHOD, map.get("goodsSource").toString()));
					if (String.valueOf(Constants.GOODS_SOURCE_BACG).equals(map.get("goodsSource").toString())){
						map.put("lsBackDrugFlag", true);
					} else {
						map.put("lsBackDrugFlag", false);
					}
				}
			}
		}
		return page;
	}
	/**
	 * 加入计划
	 */
	@Transactional
	public void batchJoinPlan(List<PurPlanForm> list){
		Session session = factory.getCurrentSession();
		Transaction tran = session.getTransaction();
		try {
			tran.begin();
			dao.batchJoinPlan(list);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}
	
	@SuppressWarnings("unchecked")
	public Pagination getPurPlanDataList(Pagination page){
		page = dao.getPurPlanDataList(page);
		page.setRows(formatPurPlan((List<Map<String,Object>>)page.getRows(),page));
		return page;
	}
	
	/**
	 * 删除计划
	 */
	@Transactional
	public void batchDelete(List<PurPlanForm> list){
		Session session = factory.getCurrentSession();
		Transaction tran = session.getTransaction();
		try {
			tran.begin();
			dao.batchDelete(list);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}
	
	/**
	 * 保存计划
	 */
	@Transactional
	public void batchSave(List<PurPlanForm> list){
		Session session = factory.getCurrentSession();
		Transaction tran = session.getTransaction();
		try {
			tran.begin();
			dao.batchSave(list);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}
	
	
	@SuppressWarnings("unused")
	private List<PurPlanForm> formatHospCatalog(List<Map<String,Object>> list){//制定计划
		List<PurPlanForm> flist = new ArrayList<PurPlanForm>();
		Map<String,Object>  map = new HashMap<String, Object>();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				PurPlanForm form = new PurPlanForm();
				map = list.get(i);
				form.setDelCompId(map.get("delCompCode")==null?"":map.get("delCompCode").toString());
				form.setProdCompId(map.get("prodCompcode")==null?"":map.get("prodCompcode").toString());
				form.setDrugId(map.get("drugCode")==null?"":map.get("drugCode").toString());
				form.setHospProCatId(map.get("hospProCatId")==null?"":map.get("hospProCatId").toString());
				form.setProLimitPrice(map.get("proPriceLimit")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("proPriceLimit").toString())));
				form.setProPrice(map.get("proPrice")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("proPrice").toString())));
				form.setDelComp(map.get("delcompname")==null?"":map.get("delcompname").toString());
				form.setDrugCode(map.get("drugCode")==null?"":map.get("drugCode").toString());
				form.setDrugName(map.get("qualityspecName")==null?"":map.get("qualityspecName").toString());
				form.setFormName(map.get("formName")==null?"":map.get("formName").toString());
				form.setSpecName(map.get("specName")==null?"":map.get("specName").toString());
				form.setFactor(map.get("factor")==null?"":map.get("factor").toString());
				form.setUnit(map.get("unitName")==null?"":map.get("unitName").toString());
				form.setMaterialName(map.get("materialName")==null?"":map.get("materialName").toString());
				form.setBakPurCount(map.get("bakPurCount")==null?"":map.get("bakPurCount").toString());
				form.setGoodsCode(map.get("goodsCode")==null?"":map.get("goodsCode").toString());
				form.setCompName(map.get("prodCompname")==null?"":map.get("prodCompname").toString());
				form.setProjectName(map.get("projName")==null?"":map.get("projName").toString());
				if (null != map.get("goodsSource")){
					form.setGoodsSource(DicSysUtil.getValueByClassCodeAndDicCode(Constants.PRO_METHOD, map.get("goodsSource").toString()));
					if (String.valueOf(Constants.GOODS_SOURCE_BACG).equals(map.get("goodsSource").toString())){
						form.setLsBackDrugFlag(true);
					}
				}
				flist.add(form);
			}
		}
		return flist;
	}
	private List<PurPlanForm> formatPurPlan(List<Map<String,Object>> list,Pagination page){//查看购物计划
		Double allPrice = 0.00;
		Map<String,String> map = new HashMap<String,String>();
		Map<String,Object> mapResult = new HashMap<String,Object>();
		List<PurPlanForm> flist = new ArrayList<PurPlanForm>();
		if (null != list && list.size() > 0){
			for (int i = 0; i < list.size(); i++){
				PurPlanForm form = new PurPlanForm();
				mapResult = list.get(i);
				form.setCartId(mapResult.get("cartId")==null?"":mapResult.get("cartId").toString());
				form.setGoodsCode(mapResult.get("goodsCode")==null?"":mapResult.get("goodsCode").toString());
				form.setDrugCode(mapResult.get("drugCode")==null?"":mapResult.get("drugCode").toString());
				form.setDrugName(mapResult.get("qualityspecName")==null?"":mapResult.get("qualityspecName").toString());
				form.setFormName(mapResult.get("formName")==null?"":mapResult.get("formName").toString());
				form.setSpecName(mapResult.get("specName")==null?"":mapResult.get("specName").toString());
				form.setFactor(mapResult.get("factor")==null?"":mapResult.get("factor").toString());
				form.setUnit(mapResult.get("unitName")==null?"":mapResult.get("unitName").toString());
				form.setMaterialName(mapResult.get("materialName")==null?"":mapResult.get("materialName").toString());
				form.setCompName(mapResult.get("prodCompname")==null?"":mapResult.get("prodCompname").toString());
				form.setDrugMedicine(mapResult.get("drugMedicine")==null?"":mapResult.get("drugMedicine").toString());
				form.setProLimitPrice(mapResult.get("proPriceLimit")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(mapResult.get("proPriceLimit").toString())));
				form.setProPrice(mapResult.get("proPrice")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(mapResult.get("proPrice").toString())));
				form.setProjectName(mapResult.get("name")==null?"":mapResult.get("name").toString());
				form.setDelComp(mapResult.get("delcompname")==null?"":mapResult.get("delcompname").toString());
				form.setDelCompId(mapResult.get("delCompCode")==null?"":mapResult.get("delCompCode").toString());
				form.setProjectName(mapResult.get("projName")==null?"":mapResult.get("projName").toString());
				if (null != mapResult.get("goodsSource")){
					/*if (Constants.GOODS_SOURCE_BACG == ParseNumber.toInteger(mapResult.get("goodsSource").toString())){
						form.setLsBackDrugFlag(true);
					}*/
					form.setGoodsSource(DicSysUtil.getValueByClassCodeAndDicCode(Constants.PRO_METHOD, mapResult.get("goodsSource").toString()));
				}
				
				
				if (null != mapResult.get("isLongtimePlan") && !"".equals(mapResult.get("isLongtimePlan").toString().trim())){
					if (IsLongtimePlan.TimePlan.getKey() == Integer.valueOf((mapResult.get("isLongtimePlan").toString().trim()))){
						form.setBakCatalog(IsLongtimePlan.TimePlan.getValue());
//						form.setLsBackDrugFlag(true);
						form.setBuyNum(mapResult.get("proCount")==null?"":mapResult.get("proCount").toString());
					} else{
						form.setBakCatalog(IsLongtimePlan.LongPlan.getValue());
//						form.setLsBackDrugFlag(true);
						form.setBuyNum(mapResult.get("proCount")==null?"":mapResult.get("proCount").toString());
							
					 }
					}
				
				form.setBuyNum(mapResult.get("proCount")==null?"":mapResult.get("proCount").toString());
				form.setDrugId(mapResult.get("goodsCode")==null?"":mapResult.get("goodsCode").toString());
				form.setJoinPlanTime(mapResult.get("joinPlanTime")==null?"":ParseDate.parseFullFormat(ParseDate.fromFullFormat(mapResult.get("joinPlanTime").toString())));
				form.setJoinPlanUser(mapResult.get("username")==null?"":mapResult.get("username").toString());
				form.setAllPrice(""+(Double.valueOf(form.getProPrice()) * 10000 * Integer.valueOf(form.getBuyNum()) / 10000));
				allPrice += Double.valueOf(form.getAllPrice());
				form.setProdCompId(mapResult.get("prodCompcode")==null?"":mapResult.get("prodCompcode").toString());
				flist.add(form);
			}
			map.put("allPrice",ParseNumber.toStrDecimal(allPrice));
		}else{
			map.put("allPrice",ParseNumber.toStrDecimal(allPrice));
		}
		page.setConditions(map);
		return flist;
	}
	/**
	 * 
	 *@category 根据ID获取已勾选目录ID
	 *@author 邱磊
	 *@date 2015年7月17日 下午4:12:51
	 *@param id
	 *@return
	 */
	public PurHospcatalog getCatalogInfoById(String id){
		return dao.getCatalogInfoById(id);
	}
	
	/**
	 * 
	 * @category 获取购物计划数量
	 * @author wangbing
	 * @date 2015年7月17日 下午4:12:51
	 * @param id
	 * @return
	 */
	@Override
	public int getPlanCount(String hospCode){
		return dao.getPlanCount(hospCode);
	}

	@Override
	public Pagination getHisRecordAmount(Pagination page) {
		return dao.getHisRecordAmount(page);
	}
}
